package edu.wpi.cs509.ui.simulators;

import edu.wpi.cs509.common.exceptions.IllegalMove;
import edu.wpi.cs509.common.exceptions.ServerUnreachable;
import edu.wpi.cs509.representation.Location;
import edu.wpi.cs509.representation.PlayerPiece;
import edu.wpi.cs509.representation.TeamColor;
import edu.wpi.cs509.ui.intfc.GameInterface;

public class AI {

	public void move( GameInterface cGame ) {
		
		// find a forward piece and move ahead
		PlayerPiece piece = null;
		Location to = null;
		
		piece = selectPiece( cGame, piece );
		
		do {
			
			to = moveForward( piece );
			
			if (to == null) {
				to = moveLeft( piece );
			}

			if (to == null) {
				to = moveRight( piece );
			}

			if (to == null) {
				to = moveBack( piece );
			}

		} while (to == null);
			
		Location from = piece.getLocation();
		
		//JFrame dialog = new JFrame();
		//JOptionPane.showMessageDialog(dialog,
		//    "Simulator moves from="+from.toString()+" to="+to.toString(), "Game Simulator",
		//    JOptionPane.INFORMATION_MESSAGE);
		
		try {
			cGame.move(from, to);
		} catch (IllegalMove e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServerUnreachable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	private Location move( PlayerPiece piece, int redX, int redY, int blueX, int blueY ) {
		int x = piece.getLocation().getXCoordinate();
		int y = piece.getLocation().getYCoordinate();
		
		if (piece.getColor() == TeamColor.BLUE) {
			x += blueX;
			y += blueY;
		} else {
			x += redX;
			y += redY;
		}
		
		// check bounds
		if ((x < 0) || (x > 9)) {
			return null;
		}
		
		if ((y < 0) || (y > 9)) {
			return null;
		}
		
		return new Location( x, y );
	}


	private Location moveForward( PlayerPiece piece ) {

		if (piece.getColor() == TeamColor.RED) {
			return move(piece, 0, 1, 0, 0);
		} else {
			return move(piece, 0, 0, 0, -1);
		}
	}

	
	private Location moveBack( PlayerPiece piece ) {

		if (piece.getColor() == TeamColor.RED) {
			return move(piece, 0, -1, 0, 0);
		} else {
			return move(piece, 0, 0, 0, 1);
		}

	}

	private Location moveLeft( PlayerPiece piece ) {

		if (piece.getColor() == TeamColor.RED) {
			return move(piece, -1, 0, 0, 0);
		} else {
			return move(piece, 0, 0, 1, 0);
		}
	}

	private Location moveRight( PlayerPiece piece ) {
		if (piece.getColor() == TeamColor.RED) {
			return move(piece, 1, 0, 0, 0);
		} else {
			return move(piece, 0, 0, -1, 0);
		}
	}

	private PlayerPiece selectPiece(GameInterface game, PlayerPiece piece) {

		ClientGame cGame = (ClientGame)game;
		
		if (piece == null) {
			if (cGame.getMyColor() == TeamColor.BLUE) {
				// start from the bottom of the board and work up to find the most forward BLUE piece
				for (int y=0; y<10; y++) {
					for (int x=0; x<10; x++) {
						piece = cGame.getServerGame().getBoardPiece(new Location(x,y));
						if (piece != null && (piece.getColor() == TeamColor.BLUE) && (piece.maxSpacesCanMove()>0)) {
							return piece;
						}
					}
				}
			} else {
				// start from the top of the board and work down to find the most forward RED piece
				for (int y=9; y>=0; y--) {
					for (int x=0; x<10; x++) {
						piece = cGame.getServerGame().getBoardPiece(new Location(x,y));
						if (piece != null && (piece.getColor() == TeamColor.RED) && (piece.maxSpacesCanMove()>0)) {
							return piece;
						}
					}
				}
			}
		} else {
			
		}
		
		return null;
	}

}
